<?php
namespace app\home\controller;
use think\Controller;
use think\Session;
use app\home\model\AddressModel;

class Address extends Controller{
    //获取当前用户的所有收货地址
    public function addressList(){
        if( Session::get('USER.id') >0 ){
            $Address=new  AddressModel();
            $rows=$Address->addressList();

            $this->assign('rows',$rows);
            return $this->fetch('address_list');
        }else{
            $this->redirect('login/login');
        }

    }
    //设为默认地址
    public function setDefault(){
        $id=input('id',0);
        $Address=new  AddressModel();
        $res=$Address->setDefault($id);
        if($res>0){
            echo 1;
        }else{
            echo -1;
        }
    }
    //删除默认地址
    public function deleteAddress(){
        $id=input('id',0);
        $Address=new  AddressModel();
        $res=$Address->deleteAddress($id);
        if($res>0){
            echo 1;
        }else{
            echo -1;
        }
    }
    //添加地址
    public function addAddress(){
        if( Session::get('USER.id') >0 ){
            $province=new  AddressModel();
            $rows=$province->getProvince();
            $this->assign('rows',$rows);
            return $this->fetch('add_address');
        }else{
            $this->redirect('login/login');
        }
    }
    //三级联动 ajax获取城市
    public function getCityId(){
        $areaId=input('areaId',0);
        $province=new  AddressModel();
        $rows=$province->getCityId($areaId);
        print_r(json_encode($rows));
    }
    //真正提交地址
    public function doAddAddress(){
        $data['user_name']=input('user_name');
        $data['phone']=input('phone');
        $data['area_id1']=input('provinceId');
        $data['area_id2']=input('cityId');
        $data['area_id3']=input('countyId');
        $data['address']=input('address');
        $data['user_id']=Session::get('USER.id');
        $province=new  AddressModel();
        $res=$province->doAddAddress($data);
        if($res>0){
            echo 1;
        }else{
            echo -1;
        }
    }
    //修改收货地址
    public function updateAddress(){
        $id=input('id',0);
        $Address=new  AddressModel();
        $rows=$Address->updateAddress($id);
        //获取省份无需判断
        $getProvince=$Address->getProvince();
        if( $rows  ){
            $getCityId=$Address->getCityId($rows['area_id1']);
            $getCountyId=$Address->getCityId($rows['area_id2']);
            $this->assign('getCityId',$getCityId);
            $this->assign('getCountyId',$getCountyId);
            $this->assign('areaid1',$rows['area_id1']);
            $this->assign('areaid2',$rows['area_id2']);
            $this->assign('areaid3',$rows['area_id3']);
        }else{
            $this->assign('getCityId',array());
            $this->assign('getCountyId',array());
            $this->assign('areaid1',0);
            $this->assign('areaid2',0);
            $this->assign('areaid3',0);
        }
        $this->assign('getProvince',$getProvince);
        $this->assign('rows',$rows);
        return $this->fetch('update_address');
    }
    //真正修改提交地址
    public function doUpdateAddress(){
        $data['user_name']=input('user_name');
        $data['phone']=input('phone');
        $data['area_id1']=input('provinceId');
        $data['area_id2']=input('cityId');
        $data['area_id3']=input('countyId');
        $data['address']=input('address');
        $where['id']=input('id');

        $province=new  AddressModel();
        $res=$province->doUpdateAddress($data,$where);
        if($res>0){
            echo 1;
        }else{
            echo -1;
        }
    }





}